package com.apes.hr.paySupport.repository;

import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.hr.base.model.Employee;
import com.apes.hr.paySupport.model.SupportApplicationExecution;
import com.apes.hr.paySupport.model.SupportApplicationItem;
import com.apes.hr.transfer.model.TransferApplicationExecution;
import com.apes.hr.transfer.model.TransferApplicationItem;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

/**
 * @Title: SupportApplicationExecution
 * @Description:付费支援申请单执行表
 * @author: ZhengZeXian
 * @create: 2021/4/15 10:10
 */
public interface SupportApplicationExecutionRepository extends CustomRepository<SupportApplicationExecution,String> {

    @Query(value = "SELECT T.*, T.ROWID FROM HR_SUPPORT_EXECUTION T WHERE T.SUPPORT_APPLICATION_ITEM_ID = ?1",nativeQuery = true)
    SupportApplicationExecution findByTransferApplicationItems(SupportApplicationItem item);

    List<SupportApplicationExecution> findBySupportUid(Employee employee);

    @Query(value = "SELECT A.* FROM HR_SUPPORT_EXECUTION A,HR_SUPPORT_APPLICATION_ITEM B WHERE A.SUPPORT_APPLICATION_ITEM_ID = B.ID AND B.REPEAL = 0 AND A.SUPPORT_UID = ?1",nativeQuery = true)
    List<SupportApplicationExecution> findBySupportApplication(@Param("employeeId") String employeeId);

    /**
     * 查询所有有效的计费支援单
     */
    @Query(value = "SELECT T.* FROM HR_SUPPORT_EXECUTION T,HR_SUPPORT_APPLICATION_ITEM A WHERE A.ID = T.SUPPORT_APPLICATION_ITEM_ID AND A.REPEAL = 0 AND A.ICON_EXECUTE = 1 AND T.END_DATE >= SYSDATE",nativeQuery = true)
    List<SupportApplicationExecution> findAllByEndDate();

    /**
     * 查询结束日期已到的未终止的 下午
     */
    @Query(value = "select T.* from hr_support_execution T,hr_support_application_item a WHERE a.id = t.support_application_item_id AND a.repeal=0 And t.end_date > ?1 and t.end_date <?2 ",nativeQuery = true)
    List<SupportApplicationExecution> findByEndDatePM(@Param("yesterday") Date yesterday, @Param("today")Date today);

    /**
     * 查询结束日期已到的未终止的 上午
     */
    @Query(value = "select T.* from hr_support_execution T,hr_support_application_item a WHERE a.id = t.support_application_item_id AND a.repeal=0 And t.end_date >= ?1 and t.end_date <= ?2",nativeQuery = true)
    List<SupportApplicationExecution> findByEndDateAM(@Param("today")Date today,@Param("todayNoon")Date todayNoon);

    /**
     * 查询开始日期已到的 上午
     */
    @Query(value = "select T.* from hr_support_execution T,hr_support_application_item a WHERE a.id = t.support_application_item_id AND a.repeal=0 And t.start_date >= ?1 and t.start_date <?2 ",nativeQuery = true)
    List<SupportApplicationExecution> findByStartDateAM(@Param("today")Date today,@Param("todayNoon")Date todayNoon);

    /**
     * 查询开始日期从下午开始的 下午
     */
    @Query(value = "select T.* from hr_support_execution T,hr_support_application_item a WHERE a.id = t.support_application_item_id AND a.repeal=0 And t.start_date >= ?1 and t.start_date <?2 ",nativeQuery = true)
    List<SupportApplicationExecution> findByStartDatePM(@Param("todayNoon")Date todayNoon,@Param("todayNoon")Date tomorrow);
}
